<?php
// start session
session_start();

//establish connection with database
$check = mysql_connect('localhost', 'root', '');
if (!$check) {
    die('Could not connect:' . mysql_error());
}
mysql_select_db("mydb", $check);

//take id of member from SignIn
$member_id = $_SESSION['member_id'];



//take id of Conf from dashboard page
$confid = intval($_GET['conference_id']);

//start function of send request to the authors


function requestAuthors($requestID, $req_text, $confid) {
    //check if the id of the member pass from the begining of the session

    if (isset($_SESSION['member_id'])) {
        //save value of member id
        $member_id = $_SESSION['member_id'];

        //save value returned the members returened from the check box
        $members = $_POST['Members'];
$done=0;
        // for loop on the members array
        for ($j = 0; $j < count($_POST['Members']); $j++) {

            //save reciver id
            $requestID = $members[$j];

            //request type A for Authors
            $A = 'A';

            // we want to get the list of members who have benn invited to review in the Conf
            $list = mysql_query("SELECT member_id2,conference_id, type From member_request_member Where member_id2 = '$requestID'
                    AND conference_id = '$confid' AND type = '$A'") or die(mysql_error());

            // assume that the query did not return any result
            if (!mysql_num_rows($list)) {

                //insert req_text into request table to get its id
                $last_req_id = mysql_query("Insert into request(request) Values ('$req_text')")
                        or die(mysql_error());

                //get last id of request which is incremented in request table
                if ($last_req_id) {
                    $last_id = mysql_insert_id();
                }
                //insert user information to the table member_request_member while check abrove null until the accept
                mysql_query("INSERT INTO member_request_member(member_id1,member_id2,conference_id,request_id,type)Values ('$member_id','$requestID','$confid','$last_id','$A')") or die("fhgfgfh" . mysql_error());

                //if the query insert the inputs correctly and it excutes a message appear to the user that his request has been sent
                $done=1;
            } else {   //a message appear to the user notify him that the members who have been selected are already invited
                echo '<script>',
                'alert("This user has already been invited to be an author in this conference before");',
                '</script>';
            }
        }
    } else {
        // a message appear when the member_id is not obtained from the session
        echo '<script>',
        'alert("This action can not be performed!");',
        '</script>';
    }
       
 
         if ($done==1)
         {
             echo '<script>',
             'alert("The request(s) has been successfully sent ");',
             '</script>';
         }
 else if ($done==0) { 
     echo '<script>',
             'alert("This user has already been invited to review in this conference before");',
             '</script>';}
             
             
 
}

// check if the Send button clicked
if (isset($_POST['Send'])) {
    // check the value inside the check box
    if (isset($_POST['Members'])) {
        // get conference name
        $get_conf_name = mysql_query("Select long_name from conference Where conference_id = '$confid'") or die(mysql_error());
        $result = mysql_fetch_assoc($get_conf_name);

        // save result
        $conf_name = $result['long_name'];

        //body of the request send to the Authors
        $req_text = 'ConfO would like to invite you to submit your academic paper in' . "$conf_name" . 
                'You can review the conference details in the conference page.';
        // call of function requestAuthors
        requestAuthors($_POST['Members'], $req_text, $confid);
    } else {
        // a message notify the user that no box is selected
        echo '<script>',
        'alert("You have to select atleast one member!");',
        '</script>';
    }
}
retrieve($member_id, $confid);

//function that will return all the members Authors or reviewers or organizers who are not reviwers in this conference
function retrieve($member_id) {

    $ret = mysql_query("Select distinct m.member_id, first_name, last_name, email FROM member m, member_privileges Where m.member_id != '$member_id' AND
(m.member_id = member_privileges.member_id) OR ( m.member_id NOT IN (Select distinct m.member_id FROM member_privileges,member m where m.member_id = member_privileges.member_id))") or die(mysql_error());


    //check output of query
    if (!mysql_num_rows($ret)) {
        echo '<script>',
        'alert("No Requests Pending");',
        '</script>';
    } else {
        print "<table width=500 border=1>\n";
        print "<tr>\n";
        print "<th>&nbsp; </th>\n";
        print "<th><width=\"0%\" height=\"0\" style=\"display:none\" value=\"Member ID\"</th>\n";
        print "<th> First Name </th>\n";
        print "<th> Last Name </th>\n";
        print "<th> e-mail </th>\n";
        print "</tr>\n";


        while ($row = mysql_fetch_assoc($ret)) {
            ?>
            <form method="post" Action =""/>

            <?php
            print "<tr>\n";
            print "<td><input type=\"checkbox\" name=\"Members[]\" value=\"{$row['member_id']}\"></td>\n";
            echo "<td><width=\"0%\" height=\"0\" style=\"display:none\" value=\"\"</td>\n";
            echo "<td>{$row['first_name']}</td>\n"; // print names
            echo "<td>{$row['last_name']}</td>\n";
            echo "<td>{$row['email']}</td>\n"; // print emails
            echo "</tr>\n";
            ?> 

            <?
        }
        ?>
        <tr><td><input type="submit" name="Send" value="Invite Authors"></td></tr>
        <br>
        <?
        print "</table>\n";
    }
}
?>

<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title>Invite Authors</title>
    </head>

</form>

</html>
